package com.spsoft.core.exception;

import com.spsoft.core.enums.ResponseEnum;
import com.spsoft.core.model.Result;
import com.spsoft.core.utils.ThrowableUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;
import java.sql.SQLTransactionRollbackException;
import java.util.Objects;

/**
 * 全局异常处理
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理所有不可知的异常
     */
    @ExceptionHandler(Throwable.class)
    public Result handleException(Throwable e) {
        // 打印堆栈信息
        log.error(ThrowableUtil.getStackTrace(e));
        return Result.ERROR();

    }

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(value = BaseException.class)
    public Result baseException(BaseException e) {
        // 打印堆栈信息
        log.error(ThrowableUtil.getStackTrace(e));
        return Result.ERROR(ResponseEnum.ERROR.getCode(), e.getMessage());
    }

    /**
     * 处理自定义业务异常
     */
    @ExceptionHandler(value = BusinessException.class)
    public Result businessException(BusinessException e) {
        // 打印堆栈信息
        log.error(ThrowableUtil.getStackTrace(e));
        return Result.ERROR(e.getCode(), e.getMsg());
    }

    /**
     * 处理非法参数异常
     */
    @ExceptionHandler(value = IllegalArgumentException.class)
    public Result illegalArgumentException(IllegalArgumentException e) {
        // 打印堆栈信息
        log.error(ThrowableUtil.getStackTrace(e));
        return Result.ERROR(ResponseEnum.ILLEGAL_ARGUMENT_ERROR.getCode(), e.getMessage());
    }

    /**
     * 处理死锁异常
     */
    @ExceptionHandler(value = SQLTransactionRollbackException.class)
    public Result baseException(SQLTransactionRollbackException e) {
        // 打印堆栈信息
        log.error(ThrowableUtil.getStackTrace(e));
        return Result.ERROR(ResponseEnum.SQL_ROLLBACK_ERROR);
    }


    @ExceptionHandler(LoginException.class)
    public Result loginException(LoginException e) {
        // 打印堆栈信息
        log.error("{}:{}",e.getCode(),e.getMsg());
        return Result.ERROR(e.getCode(),e.getMsg());
    }

    @ExceptionHandler(AuthException.class)
    public Result authException(AuthException e) {
        // 打印堆栈信息
        String message = ResponseEnum.AUTH_ERROR.equals(e.getE()) ? "用户没有权限" : e.getMessage();
        log.error(message);
        return Result.ERROR(e.getE().getCode(),message);
    }

    /**
     * 处理所有接口数据验证异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        // 打印堆栈信息
        log.error(ThrowableUtil.getStackTrace(e));
        String[] str = Objects.requireNonNull(e.getBindingResult().getAllErrors().get(0).getCodes())[1].split("\\.");
        String message = e.getBindingResult().getAllErrors().get(0).getDefaultMessage();
        String msg = "不能为空";
        if (msg.equals(message)) {
            message = str[1] + ":" + message;
        }
        return Result.ValidateError(message);
    }

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        log.error(e.getMessage(), e);
        return Result.ERROR();
    }


    @ExceptionHandler(ConstraintViolationException.class)
    public Result constraintViolationException(ConstraintViolationException e) {
        return Result.ValidateError(e.getMessage());
    }
}
